﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AppLab.Core
{
    public class Log
    {
        private static IList<LogListener> listeners = new List<LogListener>();
        //private static readonly object critSec = new object();

        public static IList<LogListener> Listeners
        {
            get
            {
                return listeners;
            }
        }

        public static void Assert(bool condition)
        {
            Assert(condition, "Assert Failed!");
        }
        public static void Assert(bool condition, string format, params object[] objs)
        {
            if (!condition)
            {
                WriteError(format, objs);
            }
        }
        public static void WriteInfoIf(bool condition, string format, params object[] objs)
        {
            if (condition)
                WriteInfo(format, objs);
        }
        public static void WriteInfo()
        {
            WriteInfo(String.Empty);
        }
        public static void WriteInfo(string format, params object[] objs)
        {
            foreach (var g in listeners)
            {
                g.WriteLine("Info", String.Format(format, objs));
                g.Flush();
            }
        }
        public static void WriteErrorIf(bool condition, string format, params object[] objs)
        {
            if (condition)
                WriteError(format, objs);
        }
        public static void WriteError(string format, params object[] objs)
        {
            foreach (var g in listeners)
            {
                g.WriteLine("Error", String.Format(format, objs));
                g.Flush();
            }
        }
        public static void WriteException(Exception e, string format, params object[] objs)
        {
            foreach (var g in listeners)
            {
                g.WriteLine("Exception", String.Format(format, objs));
                g.WriteLine("Exception", e.ToString());
                g.Flush();
            }
        }
    }
}
